<!DOCTYPE html>
<html lang="en" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
      xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <link rel="stylesheet" href="/layui/css/layui.css">
  <link rel="stylesheet" href="/css/custom.form.css">
</head>
<body>
<div class="panel panel-default operation" hidden>
  <div class="panel-heading title"></div>
<div class="layui-card-body">
<form class="layui-form " action="" lay-filter="info" style="margin-top: 10px">
      <input name="id" hidden/>
      <input name="taxorgcode" hidden/>
      <input name="stationId" hidden/>
      
     <div class="layui-form-item">
      
      <label class="layui-form-label">销售月份</label>
      <div class="layui-input-block">
        <input type="yearMonths" name="yearMonths" placeholder="请输入销售月份" autocomplete="off" class="layui-input">
      </div>
      </div>
      
      <div class="layui-form-item">
      <label class="layui-form-label">销售额</label>
      <div class="layui-input-block">
        <input type="sale" name="sale" placeholder="请输入销售额" autocomplete="off" class="layui-input">
      </div>
      </div>
      
      <div class="layui-form-item">
      <label class="layui-form-label">加油站名称</label>
      <div class="layui-input-block">
        <input type="stationName" name="stationName" placeholder="请输入加油站名称" autocomplete="off" class="layui-input">
      </div>
    </div>
    
    <div class="layui-form-item">
      <label class="layui-form-label">机关</label>
      <div class="layui-input-block">
        <input type="taxorgName" name="taxorgName" placeholder="请输入机关" autocomplete="off" class="layui-input">
      </div>
    </div>
    
    <div class="layui-form-item">
      <label class="layui-form-label">申报时间</label>
      <div class="layui-input-block">
        <input type="declaretaxTime" name="declaretaxTime" placeholder="请输入申报时间" autocomplete="off" class="layui-input">
      </div>
    </div>
    
    <div class="layui-form-item">
      <label class="layui-form-label">申报金额</label>
      <div class="layui-input-block">
        <input type="declaretaxMoney" name="declaretaxMoney" placeholder="请输入申报金额" autocomplete="off" class="layui-input">
      </div>
    </div>
        
  <div class="layui-form-item">
  <div class="layui-input-block">
    <button type="submit" class="layui-btn" lay-submit="" lay-filter="submit">保存</button>
    <button  class="layui-btn layui-btn-primary" id="btn_cancel">返回</button>
  </div>
</div>
</form>
</div>
</div>

<div class="table_div">
  <div id="searchParam">
    <div class="layui-form-item">
      <div class="layui-input-inline layui-form">
        <!-- <input type="text" id="taxorgNameInput" class="layui-input"  autocomplete="off" placeholder="请输入机关"> -->
      	<select id="taxorgcode">
		</select>
      </div>
      
	  <div class="layui-input-inline">
        <input type="text" id="stationNameInput" class="layui-input"  autocomplete="off" placeholder="请输入加油站名称">
      </div>
      
      <div class="layui-input-inline">
        <input type="text" id="createTimeInput" class="layui-input"  autocomplete="off" placeholder="请输入销售月份">
      </div>
      
      <div class="layui-input-inline ">
        <button class="layui-btn" onclick="search()"  id="search">查询</button>
      </div>
      <div class="layui-input-inline ">
	      <button class="layui-btn" onclick="exportData()"  id="export">导出</button>
	      <button class="layui-btn" id="import">批量导入</button>
      </div>
    </div>

  </div>
  <table class="layui-table" id="showTable" lay-filter="showTable" ></table>
</div>
<script type="text/html" id="toolbar">
  <!-- 
  <div class="layui-btn-container">
    <button class="layui-btn layui-btn-sm" lay-event="add" >添加</button>
    <button class="layui-btn layui-btn-sm" lay-event="batchDeleted">删除</button>
  </div>
  -->
</script>
<script type="text/html" id="tool">

  <a class="layui-btn layui-btn-xs" lay-event="edit">申报比对</a>

  <!--
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
  -->
</script>

</body>
</html>
<script src="/layui/layui.all.js"></script>
<script src="/js/core.util.js"></script>
<script>
  //获取token
  var token = CoreUtil.getData("access_token");
  //地址栏转义token中的#号
  var tokenQuery = token.replace("#", "%23");
  var $ = jQuery = layui.jquery;
	//初始当前 年月
  var currentDate = new Date();
  var currentYear = currentDate.getFullYear();
  var currentMonth = currentDate.getMonth() + 1; // 月份从0开始，所以要加1
  var yearMonthInput;
  if(10 > currentMonth > 0){
	  yearMonthInput = currentYear + "0" + currentMonth;
	  $("#createTimeInput").val(currentYear + "0" + currentMonth);
  }else{
	  yearMonthInput = currentYear + "" +currentMonth;
	  $("#createTimeInput").val(currentYear + "" + currentMonth);
  }
  
  
  var tableIns1;
  var table = layui.table;
  var form = layui.form;
  var layer = layui.layer;
  var upload = layui.upload;
  var laydate = layui.laydate;

  $(function() {
	  // 加载机关
      CoreUtil.sendGet("/sys/deptlist",null , function(res) {
			if (res.code == '0') {
				var value = res.data[0];
				var opt = "<option value = ''>请选择所属机关</option>";
				for (var i in value) {
					opt += '<option value = "' + value[i].id + '">'
							+ value[i].name + '</option>';
				}
				
				$("#taxorgcode").html(opt);
				form.render('select'); //需要渲染一下
			} else {
				layer.msg(res.msg);
				return false;
			}
		}, false, false);
		
  });  
  
  
  layui.use(['table', 'layer', 'laydate'], function () {
	
	  laydate.render({
		    elem: '#createTimeInput',
		    type: 'month', // 设置类型为年选择
		    format: 'yyyyMM', // 设置年月格式
		    trigger: 'click', // 设置触发方式
      });
	  
	  
    //加载table
    tableIns1 = table.render({
      elem: '#showTable'
      , contentType: 'application/json'
      , headers: {"authorization": token}
      , page: true //开启分页
      , url: '/gas/declaretax/list' //数据接口
      , where: {createTimeStart: yearMonthInput} // 初始化时的过滤条件
      , method: 'POST'
      , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
        return {
          "code": res.code, //解析接口状态
          "msg": res.msg, //解析提示文本
          "count": CoreUtil.isEmpty(res.data) ? 0 : res.data.total, //解析数据长度
          "data": CoreUtil.isEmpty(res.data) ? null : res.data.records //解析数据列表
        }
      }
      , cols: [
        [
          {type: 'checkbox', fixed: 'left'},
          {field: 'id', title: '主键', sort: true, hide: true},
          {field: 'name', title: '加油站名称', width: 520,sort: true},
          {field: 'taxorgName', title: '机关', width: 250,sort: true},
          
          {field: 'gatherValue', title: '采集金额', width: 110,sort: true},
          /* {field: 'forecastValue', title: '预估金额', width: 110,sort: true}, */
          {field: 'forecastValue1', title: '预估金额', width: 120,sort: true,templet: function(d) {
  				if(CoreUtil.isEmpty(d.forecastValue1)){
  					return d.forecastValue;
  				}else{
  					return d.forecastValue1;
  				}
          }},
          
          {field: 'declaretaxMoney', title: '申报金额', width: 110,sort: true},
          
          /* {field: 'declaretaxTime', title: '申报月份', width: 110,sort: true}, */
          {field: 'comparison', title: '比对结果', width: 110,sort: true},
          {field: 'yearMonths', title: '销售月份', width: 110,sort: true},
          
          {field: 'taxorgcode', title: '机关代码', width: 110, hide: true,sort: true},
          {field: 'stationId', title: '加油站ID', width: 110, hide: true,sort: true},
          
          {width: 90, toolbar: "#tool", fixed: 'right' ,title: '操作'}
        ]
      ]
      , toolbar: '#toolbar'
    });


    //表头工具
    table.on('toolbar(showTable)', function(obj){
      switch(obj.event){
        case 'batchDeleted':
          var checkStatus = table.checkStatus(obj.config.id);
          var data = checkStatus.data;
          if(data.length==0){
            layer.msg("请选择要批量删除的列");
          }else {
            var ids = [];
            $(data).each(function (index,item) {
              ids.push(item.id);
            });
            tipDialog(ids);
          }
          break;
        case 'add':
          $(".table_div").hide();
          $(".operation").show();
          $(".title").html("新增");
          break;
      };
    });
    //列操作
    table.on('tool(showTable)',function (obj) {
      var data = obj.data;
      switch (obj.event) {
        case 'del':
          var ids=[];
          ids.push(data.id);
          tipDialog(ids);
          break;
        case 'edit':
          $(".table_div").hide();
          $(".operation").show();
          $(".title").html("申报比对");
          $(".operation input[name=id]").val(data.declaretaxId);
          $(".operation input[name=taxorgcode").val(data.taxorgcode);
          $(".operation input[name=stationId").val(data.id);
          
          $(".operation input[name=yearMonths]").val(data.yearMonths);
          $(".operation input[name=yearMonths]").attr("readonly",true);
          $(".operation input[name=yearMonths]").attr("disabled", true);
          
          $(".operation input[name=sale]").val(data.gatherValue);
          $(".operation input[name=sale]").attr("readonly",true);
          $(".operation input[name=sale]").attr("disabled", true);
          
          $(".operation input[name=stationName]").val(data.name);
          $(".operation input[name=stationName]").attr("readonly",true);
          $(".operation input[name=stationName]").attr("disabled", true);
          
          $(".operation input[name=taxorgName]").val(data.taxorgName);
          $(".operation input[name=taxorgName]").attr("readonly",true);
          $(".operation input[name=taxorgName]").attr("disabled", true);
          
          $(".operation input[name=declaretaxTime]").val(data.declaretaxTime);
          $(".operation input[name=declaretaxTime]").attr("readonly",true);
          $(".operation input[name=declaretaxTime]").attr("disabled", true);
          
          $(".operation input[name=declaretaxMoney]").val(data.declaretaxMoney);
          
          $(".operation input[name=field1]").val(data.field1);
          $(".operation input[name=field2]").val(data.field2);
          $(".operation input[name=field3]").val(data.field3);
          $(".operation input[name=field4]").val(data.field4);
          $(".operation input[name=field5]").val(data.field5);
          $(".operation input[name=deleted]").val(data.deleted);
          $(".operation input[name=createId]").val(data.createId);
          $(".operation input[name=updateId]").val(data.updateId);
          $(".operation input[name=createWhere]").val(data.createWhere);
          $(".operation input[name=createTime]").val(data.createTime);
          $(".operation input[name=updateTime]").val(data.updateTime);
          break;
      }
    });

    //删除
    var tipDialog=function (ids) {
      layer.open({
        content: "确定要删除么?",
        yes: function(index, layero){
          layer.close(index); //如果设定了yes回调，需进行手工关闭
          CoreUtil.sendDelete("/gas/declaretax/delete",ids,function (res) {
            layer.msg(res.msg, {time:1000},function () {
              search();
            });
          });
        }
      });
    };
	
    
	upload.render({
		elem: '#import' //绑定元素
		,url: '/gas/declaretax/importData?authorization='+tokenQuery //上传接口
		,exts: 'xlsx' //限制上传文件
		,size:"20480"
		,before: function(obj){
			layer.msg('文件上传比对中...', {
			icon: 16,
			shade: 0.01,
			time: 0
			})
		}
		,done: function(res){
			if(res.code == "0"){
				layer.msg("导入文件比对成功！");
				search();
			}else{
				layer.msg(res.msg);
			}
	      }
	      ,error: function(){
	    	  layer.msg("文件上传失败，请稍后重试！");
	      }
	  	}); 
    
    
    //返回
    $("#btn_cancel").click(function() {
      $(".table_div").show();
      $(".operation").hide();
      return false;
    });

    //监听保存
    form.on('submit(submit)', function(data){
      if(data.field.id===undefined || data.field.id===null || data.field.id===""){
        CoreUtil.sendPost("/gas/declaretax/add",data.field,function (res) {
          $(".table_div").show();
          $(".operation").hide();
          search();
        });
      }else {
        CoreUtil.sendPut("/gas/declaretax/update",data.field,function (res) {
          $(".table_div").show();
          $(".operation").hide();
          search();
        });
      }

      return false;
    });
  });
  
  // 导出
  function exportData(){
	  var createTimeInput = $("#createTimeInput").val();
	  var taxorgNameInput = $("#taxorgNameInput").val();
	  var stationNameInput = $("#stationNameInput").val();
	  
	  var map = {
			  createTimeStart : createTimeInput,
	  			taxorgName : taxorgNameInput,
	  			stationName : stationNameInput
	  }
	  
	  CoreUtil.sendPost("/gas/declaretax/exportData",map,function (res) {
		  if(res.code == 0){
			var file = res.data[2];
			window.open(file,'_blank');
		  }else{
			layer.msg(res.msg);
			return false;
		  }
          
	  });
  }
  
  //执行查询
  function search() {
    
      var createTimeInput = $("#createTimeInput").val();
	  var stationNameInput = $("#stationNameInput").val();
	  //这里以搜索为例
    tableIns1.reload({
      where: { //设定异步数据接口的额外参数，任意设
    	  	createTimeStart : createTimeInput,
  			taxorgcode : $("#taxorgcode").val(),
  			stationName : stationNameInput
      }
      , page: {
        curr: 1 //重新从第 1 页开始
      }
    });
  };
</script>